//
//  YJCaiFuTabBar.m
//  YJB
//
//  Created by 高卫明 on 16/5/27.
//  Copyright © 2016年 YJB. All rights reserved.
//

#import "THTabBarC.h"

#import "THBBSVC.h"
#import "THNewPeeTVC.h"
#import "THPersonVC.h"
#import "THDynamicVC.h"
#import "THHomeVC.h"
#import "RDVTabBarItem.h"


@interface THTabBarC ()
@property(nonatomic,strong)NSArray *navTitles;
@end

@implementation THTabBarC

- (void)viewDidLoad
{
    [super viewDidLoad];

    self.view.backgroundColor = [UIColor whiteColor];
    //新手任务
     THNewPeeTVC *newPeeTVC = [THNewPeeTVC new];
    //论坛
     THBBSVC *bbsVC = [THBBSVC new];
    //主页
     THHomeVC *HomeVC = [THHomeVC new];
    //动态
     THDynamicVC *dynamicVC = [THDynamicVC new];
    //我的
     THPersonVC *personVC = [THPersonVC new];

    self.viewControllers = @[HomeVC, newPeeTVC,dynamicVC,bbsVC,personVC];
    
    //设置图片和标题c_home
    NSArray *images = @[@"th_home_normal",@"th_home_novice_normal",@"th_public",@"th_home_forum_normal",@"th_home_mine_normal"];
    NSArray *selectImages = @[@"th_home_selected",@"th_home_novice_select",@"th_public",@"th_home_forum_select",@"th_home_mine_select"];
    
    NSArray *titles = @[@"首页",@"新手",@"发布",@"论坛",@"我的"];
    NSInteger index = 0;
    for (RDVTabBarItem *item in self.tabBar.items)
    {
        //图片
        [item setBackgroundSelectedImage:nil withUnselectedImage:nil];
        UIImage *selectedimage = nil;
        selectedimage = [UIImage imageNamed:images[index]];
        selectedimage = [UIImage imageNamed:selectImages[index]];
        UIImage *unselectedimage = [UIImage imageNamed:images[index]];
        [item setFinishedSelectedImage:selectedimage withFinishedUnselectedImage:unselectedimage];
        //标题设置
        item.title = titles[index];
        item.selectedTitleAttributes = @{NSForegroundColorAttributeName:MYCOLOR(247, 96, 66)};
        item.unselectedTitleAttributes = @{NSForegroundColorAttributeName:MYCOLOR(114, 114, 114)};
        index++;
    }
    self.selectedIndex = 0;
//    self.navTitles = @[@"新手",@"论坛",@"首页",@"动态",@"我的"];
    self.navTitles = titles;
    //显示首页标题
    self.navigationItem.title = self.navTitles[self.selectedIndex];
    //导航栏设置
    [self setupNav];
//    //标签栏分割线
     UIView *tabBarToplineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0.5, IPHONE_W, 0.5)];
    tabBarToplineView.backgroundColor = kUIColorFromRGB(0xff783c);
    [self.tabBar addSubview:tabBarToplineView];
}


- (void)setupNav
{
    //导航栏背景图片
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBg"] forBarMetrics:UIBarMetricsDefault];
    //导航栏文字颜色
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    //导航栏标题颜色
    [self.navigationController.navigationBar setTitleTextAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:20.0f],NSForegroundColorAttributeName:[UIColor whiteColor]}];
    //导航栏返回按钮
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(backItemClick) image:@"back_white" highImage:nil title:nil];
    
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(signEarnMoneyClick) image:@"th_home_sign" highImage:@"th_home_signed" title:nil];
}

- (void)signEarnMoneyClick
{
    [[NSNotificationCenter defaultCenter]
     postNotificationName:THSignEarnMoneyNotification object:nil];
}

- (void)backItemClick
{
    [self.navigationController popViewControllerAnimated:YES];
}


//自身代理方法
-(void)tabBar:(RDVTabBar *)tabBar didSelectItemAtIndex:(NSInteger)index
{
    //标题
    self.navigationItem.title = self.navTitles[index];
    if (index != 0)
    {
        self.navigationItem.rightBarButtonItem = nil;
    }else
        self.navigationItem.rightBarButtonItem = [UIBarButtonItem itemWithTarget:self action:@selector(signEarnMoneyClick) image:@"th_home_sign" highImage:nil title:nil];
    
    
    
    [self.navigationController setNavigationBarHidden:index == 4];
     
    [super tabBar:tabBar didSelectItemAtIndex:index];
}

-(BOOL)tabBar:(RDVTabBar *)tabBar shouldSelectItemAtIndex:(NSInteger)index
{
    return  [super tabBar:tabBar shouldSelectItemAtIndex:index];
}


@end
